#include "clientinfo.h"
#include <argz.h>
#include <stdlib.h>
#include <string.h>
#include <search.h>
#include "../pwzd.h"
#include "hello.h"
#include "../hashtbl.h"
///**
// * returns empty, null-initialized client_info struct
// */
//struct client_info * create_info()
//{
//	return calloc(1, sizeof(struct client_info));
//}

//int client_compare(const void * cil, const void * cir)
//{
//    const struct client_info *l = (struct client_info *)cil;
//    const struct client_info *r = (struct client_info *)cir;
//    if (!l)
//	return -1;
//    else if (!r)
//	return 1;
//    else
//	return strcmp(l->name, r->name);
//}

//void add_client(struct client_info * info)
//{
//    tsearch(info, (void**)&others, client_compare);
//    prepend(&(tab[0]), info);
//}

//void prepend(struct client_info **list, struct client_info* info)
//{
//    // obsłuż pustą listę
//    if (!list) {
//        list = malloc(sizeof(struct clientinfo *));
//        *list = info;
//        return;
//    }
//    // obsłuż niepustą listę
//    info->next = *list;
//    *list = info;
//}

///**
// * returns initialized client_info
// */
//struct client_info * create_finfo(struct client_node *list, char *name)
//{
//	struct client_info * c;
//	c = create_info();
//    c->name          = strdup(name);
//	c->interest_list = list;
//	return c;
//}

///**
// * return NULL if not found, valid pointer to a node otherwise
// */
//struct client_node * find(struct client_node *list, char *interest)
//{
//	struct client_node *p;
//    for (p = list; p; p = p->next)
//		if (strcmp(interest, p->label)) break;
//	return p;
//}

///**
// * inserts element new element containing interest, to the list.
// */
//void insert(struct client_node **list, char * interest)
//{
//	struct client_node *p,
//	                   *n;

//	p = calloc(1, sizeof(struct client_node));
//	// handle empty list case
//	if (!(*list)) {
//		*list = p;
//		p->label = strdup(interest);
//		return;
//	}

//	p->next = *list;
//	for (; p->next; p = p->next)
//		if ( strcmp(p->label, interest) > 0 ) break;
	
//	n = calloc(1, sizeof(struct client_node));
//	n->next = p->next;

//	if (p->next == *list) {
//		*list = n;
//	} else {
//		n->next = p->next;
//		p->next = n;
//	}
//}

//struct client_node * removei(struct client_node **list, char * interest)
//{
//	struct client_node *p,*tmp;

//	p = calloc(1, sizeof(struct client_node));
	
//	// empty list case
//	if (!(*list)) return NULL;

//	// non-empty list case
//	p->next = *list;
//	for(; p->next; p = p->next)
//		if ( strcmp(p->next->label, interest) == 0 ) break;

//	// if not found
//	if (!p->next) {
//		return NULL;
//	}

//	// if is first element
//	if (p->next == *list) {
//		tmp   = *list;
//		*list = p->next->next;
//	} else {
//		tmp = p->next;
//		p->next = p->next->next;
//	}

//	return tmp;
//}

/**
 * returns list length
 */
uint32_t length(HASHTBL * hash)
{
    uint32_t i, j;
    struct hashnode_s *node;
    for( i = 0, j = 0; i < hash->size; ++i )
        for ( node = (hash->nodes)[i];
              node; node = node->next, ++j );

    return j;
}

/**
 * returns sum of label lengths (including every '\0' character)
 */
uint32_t labsize(HASHTBL * hash)
{
    uint32_t i, j;
    struct hashnode_s *node;
    for( i = 0, j = 0; i < hash->size; ++i ) {
        for ( node = (hash->nodes)[i];
              node; node = node->next ) {
            j += 1+strlen(node->key);
        }
    }
    return j;
}
